package com.atguigu.tingshu.user.api;

import com.atguigu.tingshu.common.login.annotation.TingshuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Tag(name = "微信授权登录接口")
@RestController
@RequestMapping("/api/user/wxLogin")
@Slf4j
public class WxLoginApiController {

    @Autowired
    private UserInfoService userInfoService;
    @GetMapping("/wxLogin/{code}")
    @Operation(description = "登录")
    public Result wxLogin(@PathVariable(value = "code") String code){
        Map<String,Object> map= userInfoService.wxLogin(code);
        return Result.ok(map);
    }

    @GetMapping("/refreshToken")
    @Operation(description = "刷新token")
    public Result refreshToken(){
        Map<String,Object> map=userInfoService.refreshToken();
        return Result.ok(map);
    }

    //http://localhost:8500/api/user/wxLogin/getUserInfo

    @GetMapping("/getUserInfo")
    @Operation(description = "获取用户登录信息")
    @TingshuLogin
    public Result getUserInfo(){
//        ThreadLocal<Long> longThreadLocal = LoginAspect.longThreadLocal;
//        Long userId = longThreadLocal.get();
        Long userId = AuthContextHolder.getUserId();
        UserInfoVo userInfoVo=userInfoService.getUserInfo(userId);
        return Result.ok(userInfoVo);
    }
    //http://localhost:8500/api/user/wxLogin/updateUser
    @PostMapping("/updateUser")
    @Operation(description = "修改用户登录信息")
    @TingshuLogin
    public Result updateUserInfo(@RequestBody UserInfoVo userInfoVo){
        userInfoService.updateUserInfo(userInfoVo);
        return Result.ok();
    }
}
